﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SmartMotion.Core;
namespace E5032Lib
{
    /// <summary>
    /// 雷赛的E5032运动控制卡
    /// </summary>
    public class E5032:MotionControlBase
    {
        public E5032()
        {

        }
   
        public override int Init()
        {
            return LTDMC.dmc_board_init();
        }

        public override int Close()
        {
            return LTDMC.dmc_board_close();
        }

        public override int EmgStop()
        {
           return LTDMC.dmc_emg_stop(CardID);
        }

        public override int GetErrcode(out int errcode)
        {
            errcode = -1;
            return -1;
        }

        public override int ClearErrcode()
        {
            throw new NotImplementedException();
        }

        public override int Reset()
        {
            return LTDMC.dmc_soft_reset(CardID);
        }

        public override List<AxisBase> CreateAxisses(int axisCount)
        {
            for (int i = 0; i < axisCount; i++)
            {
                var axis = new Axis(CardID, (ushort)i);
                axis.CardID = CardID;
                axis.AxisID = (ushort)i;
                Axises.Add(axis);
            }

            return Axises;
        }

        public override int SetGroupSpeed(ushort @group, double minVel, double maxVel, double acc, double dec, double stopVel)
        {
            return LTDMC.dmc_set_vector_profile_unit(CardID, group, minVel, maxVel, acc, dec, stopVel);
        }

        public override int SetGroupSpeed(ushort @group, Speed speed)
        {
            return LTDMC.dmc_set_vector_profile_unit(CardID, @group, speed.MinVel, speed.MaxVel, speed.Acc, speed.Dec, speed.StopVel);
        }

        public override int SetGroupSpeed(ushort @group, double rate)
        {
            //使用群组内速度最慢的轴速度除以比例
            throw new NotImplementedException();
        }

        public override int GetGroupSpeed(ushort @group, out double minVel, out double maxVel, out double acc, out double dec,
            out double stopVel)
        {
            stopVel = 0;
            minVel = 0;
            maxVel = 0;
            acc = 0;
            dec = 0;
            return LTDMC.dmc_get_vector_profile_unit(CardID, group, ref minVel, ref maxVel, ref acc, ref dec,
                ref stopVel);
        
        }

        public override int GetGroupSpeed(ushort @group, out Speed speed)
        {
          double stopVel = 0;
          double minVel = 0;
          double maxVel = 0;
          double acc = 0;
          double dec = 0;
          var ret= LTDMC.dmc_get_vector_profile_unit(CardID, group, ref minVel, ref maxVel, ref acc, ref dec,
                ref stopVel);
          speed.MinVel = minVel;
          speed.MaxVel = maxVel;
          speed.Acc = acc;
          speed.Dec = dec;
          speed.StopVel = stopVel;

          return ret;
        }

        public override int GetGroupSpeed(ushort @group, out double rate)
        {
            throw new NotImplementedException();
        }

        public override int LineMove(ushort @group, ushort axisNum, ushort[] axisList, double[] targetPos, int posiMode)
        {
            return LTDMC.dmc_line_unit(CardID, group, axisNum, axisList, targetPos, (ushort)posiMode);
        }   

        public override int CheckDoneMultiAxis(ushort @group)
        {
            return LTDMC.dmc_check_done_multicoor(CardID, group);
        }

        public override bool InitIoCtrl()
        {
            return true;
        }

        public override bool CloseIoCtrl()
        {
            return true;
        }

        public override int ReadInBit(ushort bitNo)
        {
           return LTDMC.dmc_read_inbit(CardID, bitNo);
        }

        public override int ReadOutBit(ushort bitNo)
        {
            return LTDMC.dmc_read_outbit(CardID, bitNo);
        }

        public override int ReadInPort(ushort portNo)
        {
            return (int)LTDMC.dmc_read_inport(CardID, portNo);
        }

        public override int ReadOutPort(ushort portNo)
        {
            return (int)LTDMC.dmc_read_outport(CardID, portNo);
        }

        public override int WriteOutbit(ushort bitNo, ushort onOff)
        {
            return LTDMC.dmc_write_outbit(CardID, bitNo, onOff);
        }

        public override int WriteOutPort(ushort portNo, ushort portValue)
        {
            return LTDMC.dmc_write_outport(CardID, portNo, portValue);
        }
    }
}
